<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\News\Video;
use App\Models\News\VideosList;
use App\Models\News\News;
use App\Models\News\NewsClass;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use Encore\Admin\Widgets\Table;
use App\Models\Artists\Artist;
use App\Models\News\Tag;
class VideoController extends Controller
{
    protected $name = '视频管理';
    use ModelForm;
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
	
	
	
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Video::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('title', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
            $grid->column('id', 'ID')->sortable();
            $grid->column('listorder', '排序')->editable();
            
            $grid->column('title', '名称');
			$grid->column('news_class.name', '栏目');
			$grid->column('urls', '引入链接');
            $grid->column('admin_user.name', '发布');
            $grid->column('admin_edit_user.name', '最后更新');
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(Video::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '标题')->rules('required|min:1');
            $form->multipleSelect('tags', '关键词')->options(function($ids){
                if($ids)
                    return Tag::find($ids)->pluck('name', 'id');
                else
                    return [];
            })->ajax('/admin/news/api/tags')->help('大写字母C，放在最后可以创建该关键词，例如：合肥C, 艺术家C');
            $form->text('description', '描述');
            $form->multipleSelect('artists', '关联艺术家')->options(function($ids){
                if($ids)
                    return Artist::find($ids)->pluck('name', 'id');
                else
                    return [];
            })->ajax('/admin/artist/api/index');
            $form->text('source', '来源');
            $form->text('author', '作者');
			$form->select('class_id', '栏目')->options(NewsClass::selectOptions())->rules('required');
            $form->radio('status','审核状态')->options(News::$status)->default('2');
			$form->image('thumb', '视频封面')->uniqueName();
			$form->text('files', '视频地址');
			
            
        });
    }
}
